package com.astn.bks;

import com.astn.bks.util.BksConstants;

import android.app.Activity;
import android.app.TabActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.TabHost.TabContentFactory;
import android.widget.TabHost.TabSpec;

public class ProviderHomeTabHost extends TabActivity {
	private TabHost mTabHost;

	private void setupTabHost() {
		mTabHost = (TabHost) findViewById(android.R.id.tabhost);
		mTabHost.setup();
	}

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// construct the tabhost
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.ptabhost);

		setupTabHost();
		mTabHost.getTabWidget().setDividerDrawable(R.drawable.tab_divider);
		initialLatestRequest(new TextView(this));
		initialMyOffer(new TextView(this));
		// setupTab(new TextView(this), "My offer");
		// setupTab(new TextView(this), "ABC");
		// initialTopProviderTab(new TextView(this));
		// initialTopDestination(new TextView(this));
		// initialMyRequest(new TextView(this));
	}

	private void setupTab(final View view, final String tag) {
		View tabview = createTabView(mTabHost.getContext(), tag);

		TabSpec setContent = mTabHost.newTabSpec(tag).setIndicator(tabview)
				.setContent(new TabContentFactory() {
					public View createTabContent(String tag) {
						return view;
					}
				});
		mTabHost.addTab(setContent);

	}

	private void initialLatestRequest(final View view) {
		View tabview = createTabView(mTabHost.getContext(), "Latest request");

		TabSpec setContent = mTabHost
				.newTabSpec("Top provider")
				.setIndicator(tabview)
				.setContent(
						new Intent(ProviderHomeTabHost.this,
								PLatestRequest.class));
		mTabHost.addTab(setContent);

	}

	private void initialMyOffer(final View view) {
		View tabview = createTabView(mTabHost.getContext(), "My offer");

		TabSpec setContent = mTabHost
				.newTabSpec("My offer")
				.setIndicator(tabview)
				.setContent(
						new Intent(ProviderHomeTabHost.this, PMyOffer.class));
		mTabHost.addTab(setContent);

	}

	private void initialMyRequest(final View view) {
		View tabview = createTabView(mTabHost.getContext(), "My request");

		TabSpec setContent = mTabHost
				.newTabSpec("My request")
				.setIndicator(tabview)
				.setContent(
						new Intent(ProviderHomeTabHost.this, CMyRequest.class));
		mTabHost.addTab(setContent);

	}

	private static View createTabView(final Context context, final String text) {
		View view = LayoutInflater.from(context)
				.inflate(R.layout.tabs_bg, null);
		TextView tv = (TextView) view.findViewById(R.id.tabsText);
		tv.setText(text);
		return view;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.optmenuprovider, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.mnProviderMessage:
			pMessage();
			return true;
		case R.id.mnProviderHistory:
			pHistory();
			return true;
		case R.id.mnProviderPayment:
			pPayment();
			return true;
		case R.id.mnProviderSearch:
			pSearch();
			return true;
		case R.id.mnProviderHelp:
			pHeld();
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	private void pPostOffer() {

	}

	private void pMessage() {
		Intent i = new Intent(ProviderHomeTabHost.this, Message.class);
		startActivityForResult(i, BksConstants.PROVIDER_MESSAGE);
	}

	private void pHistory() {

	}

	private void pPayment() {

	}

	private void pSearch() {

	}

	private void pHeld() {

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
	}

}
